﻿using CompanyManageSystem.DLL;
using CompanyManageSystem.Model;
using CompanyManageSystem.Service;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Base;
using gregn6Lib;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Entity;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CompanyManageSystem.UI.Info
{
    //公司信息界面
    public partial class Frm_Company : BaseXtraForm
    {
        #region 属性
        /// <summary>
        /// 获取当前点击的公司
        /// </summary>
        Company FocusCompany
        {
            get
            {
                return CompanyView.GetFocusedRow() as Company;
            }
        }

        /// <summary>
        /// 获取当前点击的员工
        /// </summary>
        CompanyStaffInfo FocusStuff
        {
            get
            {
                return StaffView.GetFocusedRow() as CompanyStaffInfo;
            }
        }
        #endregion

        #region 构造方法
        /// <summary>
        /// 构造方法
        /// </summary>
        public Frm_Company()
        {
            InitializeComponent();

        }
        #endregion

        #region 窗体加载完成
        /// <summary>
        /// 窗体加载完成
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Frm_ShowInfo_Load(object sender, EventArgs e)
        {
            //加载数据
            AppService aservice = new AppService();
            CompanyGrid.DataSource = aservice.GetCompanys();
            //公司的焦点行事件里面给StaffGrid添加了数据，这里不需要再加载
            //StaffGrid.DataSource = aservice.GetStaffsByCompanyID(FocusCompany.ID);
            repositoryItemSearchLookUpEdit1.DataSource = aservice.GetAllProvinces();
            repositoryItemSearchLookUpEdit2.DataSource = aservice.GetDepartments();
        }
        #endregion

        #region 公司
        /// <summary>
        /// 添加公司
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            //Frm_AddCompany frm = new Frm_AddCompany(Frm_AddCompany.EStatus.Add);
            //frm.SaveEvent += company_AddSaveEvent;
            //frm.ShowDialog();
        }
        private void company_AddSaveEvent(Object sender, EventArgs e)
        {
            AppService aservice = new AppService();
            CompanyGrid.DataSource = aservice.GetCompanys();
        }

        /// <summary>
        /// 修改公司
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Frm_AddCompany frm = new Frm_AddCompany(id: FocusCompany.ID, status: Frm_AddCompany.EStatus.Modify);
            frm.SaveEvent += company_UpdateSaveEvent;
            frm.ShowDialog();
        }
        private void company_UpdateSaveEvent(Object sender, EventArgs e)
        {
            Company com = sender as Company;
            if (com != null)
            {
                CompanyView.SetFocusedRowCellValue("CName", com.CName);
                CompanyView.SetFocusedRowCellValue("IsEnabled", com.IsEnabled);
            }
            else
            {
                AppService aservice = new AppService();
                CompanyGrid.DataSource = aservice.GetCompanys();
            }

        }

        /// <summary>
        /// 删除公司
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem3_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (FocusCompany.IsEnabled != true)
            {
                if (DialogResult.OK == MessageBox.Show("是否删除【" + FocusCompany.CName + "】公司?", "温馨提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information))
                {
                    //确定
                    AppService aservice = new AppService();
                    //aservice.DelCompany(FocusCompany.ID);
                    CompanyView.DeleteRow(CompanyView.FocusedRowHandle);

                    //因为点击确定完后没有执行公司的焦点改变事件，所以要加这句代码
                    StaffGrid.DataSource = aservice.GetStaffsByCompanyID(FocusCompany.ID);

                    MessageBox.Show("删除成功!");
                }
                else
                {
                    //取消
                }
            }
            else
            {
                MessageBox.Show("正在启用中...");
            }
        }

        /// <summary>
        /// 右键点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CompanyGrid_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                popupMenu1.ShowPopup(Control.MousePosition);
            }
        }

        #region 公司表格事件
        /// <summary>
        /// 焦点改变事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CompanyView_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            AppService aservice = new AppService();
            StaffGrid.DataSource = aservice.GetStaffsByCompanyID(FocusCompany.ID);
        }
        #endregion

        #endregion

        #region 员工
        /// <summary>
        /// 添加员工
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem4_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            //Frm_AddStaff frm = new Frm_AddStaff(companyID: FocusCompany.ID, status: Frm_AddStaff.EStatus.Add);
            //frm.SaveEvent += Staff_AddSaveEvent;
            //frm.ShowDialog();
        }
        private void Staff_AddSaveEvent(Object sender, EventArgs e)
        {
            AppService aservice = new AppService();
            StaffGrid.DataSource = aservice.GetStaffsByCompanyID(FocusCompany.ID);
        }

        /// <summary>
        /// 编辑员工
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem5_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            //Frm_AddStaff frm = new Frm_AddStaff(companyID: FocusCompany.ID, staffID: FocusStuff.ID, status: Frm_AddStaff.EStatus.Modify);
            //frm.SaveEvent += Staff_UpdateSaveEvent;
            //frm.ShowDialog();
        }
        private void Staff_UpdateSaveEvent(Object sender, EventArgs e)
        {
            AppService aservice = new AppService();
            CompanyStaffInfo info = sender as CompanyStaffInfo;
            //获取焦点行句柄
            //int handle = StaffView.FocusedRowHandle;
            if (info != null)
            {
                //第一个参数指的是FileName，即数据库中的列名
                StaffView.SetFocusedRowCellValue("FName", info.FName);
                StaffView.SetFocusedRowCellValue("Post", info.Post);
                StaffView.SetFocusedRowCellValue("Wage", info.Wage);
                StaffView.SetFocusedRowCellValue("ProvinceID", info.ProvinceID);
                StaffView.SetFocusedRowCellValue("Graduation", info.Graduation);
            }
            else
            {
                aservice = new AppService();
                StaffGrid.DataSource = aservice.GetStaffsByCompanyID(FocusCompany.ID);
            }
        }

        /// <summary>
        /// 删除员工
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem6_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (DialogResult.OK == MessageBox.Show("是否删除【" + FocusStuff.FName + "】员工?", "温馨提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information))
            {
                //确定
                AppService aservice = new AppService();
                aservice.DelStaff(FocusStuff.ID);
                StaffView.DeleteRow(StaffView.FocusedRowHandle);
                MessageBox.Show("删除成功!");
            }
            else
            {
                //取消
            }
        }

        /// <summary>
        /// 右键事件(增，删，改)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void StaffGrid_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                popupMenu2.ShowPopup(Control.MousePosition);
            }
        }

        /// <summary>
        /// 权限管理(无功能)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem9_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            MessageBox.Show("权限管理");
        }

        /// <summary>
        /// StaffView表格内容改变事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void StaffView_CellValueChanged(object sender, CellValueChangedEventArgs e)
        {
            AppService aservice = new AppService();
            //判断当前修改的是ProvinceID列
            if (e.Column.FieldName == "ProvinceID")
            {
                //获取某一行中的某个列的值
                //获取provinceID
                Object provinceID = this.StaffView.GetRowCellValue(StaffView.FocusedRowHandle, "ProvinceID");
                aservice.UpdateStaffByID(FocusStuff.ID, (Guid)provinceID);
                //MessageBox.Show("编辑成功!");
            }
        }
        #endregion

        #region 搜索功能
        /// <summary>
        /// 搜索功能
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btn_Search_Click(object sender, EventArgs e)
        {
            //获取编辑框信息
            string text = textEdit1.Text;

            //获取GridControl的焦点View
            ColumnView view = (ColumnView)StaffGrid.FocusedView;

            //GridControl里面的列
            GridColumn column = null;

            //编辑框不为空
            if (!string.IsNullOrEmpty(text))
            {
                //英文开头
                if (text[0] > 'a' && text[0] < 'z' || text[0] > 'A' && text[0] < 'Z')
                {
                    column = view.Columns["Post"];
                }
                //中文开头
                else
                {
                    column = view.Columns["FName"];
                }
            }
            else
            {
                MessageBox.Show("请输入姓名或职位");
            }

            if (column != null)
            {
                int rowHandle = 0;

                //从第0行开始查询,查询的列,匹配列中的值
                rowHandle = view.LocateByDisplayText(0, column, text);

                //如果行值正常，即满足条件，存在
                if (rowHandle != DevExpress.XtraGrid.GridControl.InvalidRowHandle)
                {
                    view.FocusedRowHandle = rowHandle;
                    view.FocusedColumn = column;
                    view.SelectRow(rowHandle);
                }
                else
                {
                    MessageBox.Show("该用户或职位不存在!");
                }
            }

        }

        /// <summary>
        /// 行风格设置(这里可以不用写，但是公司的系统要写)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void StaffView_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
        {
            //如果行不是焦点行，则背景色为白色
            //if (e.RowHandle != UserManagementView.FocusedRowHandle)
            //{
            //    e.Appearance.BackColor = Color.White;
            //}
        }
        #endregion

        #region 刷新(员工)
        /// <summary>
        /// 刷新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem10_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            AppService aservice = new AppService();
            StaffGrid.DataSource = aservice.GetStaffsByCompanyID(FocusCompany.ID);
        }
        #endregion

        private void StaffView_ShowingEditor(object sender, CancelEventArgs e)
        {


            //if (ID != null && !string.IsNullOrEmpty(ID.ToString()))
            //{
            //    //e.Cancel = true;
            //}
        }




    }
}
